package CutLoad.mc
{
	import CutLoad.Base;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * 操作影片类
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */
	public class OperationMc
	{
		/**
		 * 从库里调出一个无件，放完后自动清除 
		 * @param className 元件类名
		 * @param xx x坐标
		 * @param yy y坐标
		 * @param spParent 出现的父类
		 * 
		 */		
		public static function AttachMovieClassNamePlayEndKill(className:String,xx:Number,yy:Number,spParent:Sprite):void{
			var mcTem:MovieClip=Base.getMcFromStrClass(className);
			mcTem.x=xx;
			mcTem.y=yy;
			spParent.addChild(mcTem);
			var opTem:OperationMc=new OperationMc(mcTem,false);
			opTem.setFunEndFrame(function(target:MovieClip,me:OperationMc):void{
				target.parent.removeChild(target);
				me.killMe();
			});
		}
		/**
		 * 设置一个影片，放完后自动清除 
		 * @param target 影片
		 * @param xx x坐标
		 * @param yy y坐标
		 * @param spParent 出现的父类,可以设为空，如果它已经显示出来了。
		 * 
		 */		
		public static function MoviePlayEndKill(target:MovieClip,xx:Number,yy:Number,spParent:Sprite=null):void{
			target.x=xx;
			target.y=yy;
			if(target.parent!=null) spParent.addChild(target);
			var opTem:OperationMc=new OperationMc(target,false);
			opTem.setFunEndFrame(function(target:MovieClip,me:OperationMc):void{
				target.parent.removeChild(target);
				me.killMe();
			});
		}
		private var target:MovieClip;
		private var FunEndFrame:Function=null;
		//影片标签
		private var funFlag:Function=null;
		public function OperationMc(target:MovieClip,isStopFrame1:Boolean=true,isVisible:Boolean=true)
		{
			this.target=target;
			if(isStopFrame1) target.gotoAndStop(1);
			this.target.visible=isVisible;
		}
		/**
		 * 获得影片 
		 * @return 
		 * 
		 */		
		public function get mcTarget():MovieClip{
			return this.target;
		}
		/**
		 * 当前帧 
		 * @return 
		 * 
		 */		
		public function get currentFrame():Number{
			return this.target.currentFrame;
		}
		/**
		 * 从指定帧开始播放 
		 * @param frame 帧数或帧标签
		 * 
		 */		
		public function gotoAndPlay(frame:Object):void{
			this.target.gotoAndPlay(frame);
			target.visible=true;
		}
		/**
		 * 显示对象是否可见 
		 * @param isOk
		 * 
		 */		
		public function set visible(isOk:Boolean):void{
			this.target.visible=isOk
		}
		/**
		 * 显示对象是否可见 
		 * @return 
		 * 
		 */		
		public function get visible():Boolean{
			return this.target.visible;
		}
		/**
		 * 设置当影片放到结尾时，要调用的额外的方法 
		 * @param endFrame 格式：function(target:MovieClip,me:OperationMc)
		 * 
		 */		
		public function setFunEndFrame(endFrame:Function):void{
			this.FunEndFrame=endFrame;	
			if(!target.hasEventListener(Event.ENTER_FRAME)) target.addEventListener(Event.ENTER_FRAME,enterFrameEvent);
		}
		/**
		 * 设置影片当前帧标签 
		 * @param fun 格式:function(me:MovieClip,frameLabel:String)
		 * 
		 */		
		public function setFunPlayFrameFlag(fun:Function):void{
			this.funFlag=fun;
			if(!target.hasEventListener(Event.ENTER_FRAME)) target.addEventListener(Event.ENTER_FRAME,enterFrameEvent);
		}
		private function enterFrameEvent(e:Event):void{
			if(FunEndFrame!=null){
				//trace(this.target.currentFrame+"::"+this.target.totalFrames)
				if(this.target.currentFrame==this.target.totalFrames){
					FunEndFrame(this.target,this);
				}	
			}
			if(funFlag!=null) funFlag(this.target,this.target.currentLabel);
		}
		public function killMe():void{
			if(FunEndFrame!=null) target.removeEventListener(Event.ENTER_FRAME,enterFrameEvent);
		}
	}
}